package com.flnet.api.service.impl;

import java.util.List;

import com.flnet.api.domain.Member;
import com.flnet.common.utils.RRException;
import com.flnet.common.validator.Assert;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flnet.api.mapper.MemberMapper;
import com.flnet.api.service.IMemberService;
import com.flnet.common.support.Convert;

/**
 * 会员 服务层实现
 * 
 * @author flnet
 * @date 2019-03-27
 */
@Service
public class MemberServiceImpl implements IMemberService
{
	@Autowired
	private MemberMapper memberMapper;

	/**
     * 查询会员信息
     * 
     * @param memberId 会员ID
     * @return 会员信息
     */
    @Override
	public Member selectWMemberById(Long memberId)
	{
	    return memberMapper.selectWMemberById(memberId);
	}
	
	/**
     * 查询会员列表
     * 
     * @param member 会员信息
     * @return 会员集合
     */
	@Override
	public List<Member> selectWMemberList(Member member)
	{
	    return memberMapper.selectWMemberList(member);
	}
	
    /**
     * 新增会员
     * 
     * @param member 会员信息
     * @return 结果
     */
	@Override
	public int insertWMember(Member member)
	{
		member.setPassword(DigestUtils.sha256Hex(member.getPassword()));
	    return memberMapper.insertWMember(member);
	}
	
	/**
     * 修改会员
     * 
     * @param member 会员信息
     * @return 结果
     */
	@Override
	public int updateWMember(Member member)
	{
	    return memberMapper.updateWMember(member);
	}

	/**
     * 删除会员对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteWMemberByIds(String ids)
	{
		return memberMapper.deleteWMemberByIds(Convert.toStrArray(ids));
	}

	public long login(String mobile, String password) {
		Member user = queryByMobile(mobile);
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if (!user.getPassword().equals(DigestUtils.sha256Hex(password))) {
			throw new RRException("手机号或密码错误");
		}

		return user.getMemberId();
	}

	public Member queryByMobile(String mobile) {
		return memberMapper.queryByMobile(mobile);
	}

	public Member queryByOpenId(String openId){
		return memberMapper.queryByOpenId(openId);
	}
}
